package notification;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import test.pi.TestInterfacePackage.user;

import ibatis.IbatisHelper;

import Eclass.ExamPrepModel;

import com.ibatis.sqlmap.client.SqlMapClient;

public class NotifManager {
	private SqlMapClient ibatis;

	public NotifManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}
	
	public  List getNotifications(String userId){
		List result = null;
		
		try{
			result = ibatis.queryForList("notification.getLogNotifNotReadByUserId", userId);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public  LogNotifModel getLogNotifById(Integer logNotifId){
		LogNotifModel result = null;
		
		try{
			result = (LogNotifModel) ibatis.queryForObject("notification.getLogNotifById", logNotifId);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public  Integer getNumOfNotif(String userId){
		Integer result = null;
		
		try{ 
			result = (Integer) ibatis.queryForObject("notification.getNumOfLogNotifByUserIdAndIsRead", userId);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public Integer getLastNumberLogNotifSeq(){
		Integer result = null;
		
		try{
			result = (Integer) ibatis.queryForObject("notification.getLastNumberLogNotifSeq", "");
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return result;
	}
	
	public Boolean insertLogNotifForum(LogNotifModel bean){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.insert("notification.addLogNotifForum", bean);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean insertLogNotifMateri(LogNotifModel bean){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.insert("notification.addLogNotifMateri", bean);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean insertNotifForum(){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			Integer logNotifId = getLastNumberLogNotifSeq();
			System.out.println("**** insertNotif - logNotifId : "+logNotifId);
			this.ibatis.insert("notification.addNotifForum", logNotifId);
			System.out.println("success..!!!");
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean insertNotifMateriIndividu(String userId){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			Integer logNotifId = getLastNumberLogNotifSeq();
			Map map = new HashMap();
			map.put("logNotifId", logNotifId);
			map.put("userId", userId);
			this.ibatis.insert("notification.addNotifMateriIndividu", map);
			System.out.println("success..!!!");
			ibatis.commitTransaction();
			System.out.println("************insertNotifMateriIndividu");
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean insertNotifMateriTrainee(){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			Integer logNotifId = getLastNumberLogNotifSeq();
			this.ibatis.insert("notification.addNotifMateriTrainee", logNotifId);
			ibatis.commitTransaction();
			System.out.println("************insertNotifMateriTrainee");
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean insertNotifMateriTrainer(){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			Integer logNotifId = getLastNumberLogNotifSeq();
			this.ibatis.insert("notification.addNotifMateriTrainer", logNotifId);
			ibatis.commitTransaction();
			System.out.println("************insertNotifMateriTrainer");
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	

	public Boolean updateNotificationRead(String userId){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.update("notification.updateReadNotif", userId);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
	public Boolean updateNotificationReadById(Integer notifId){
		Boolean cek = true;
		try{
			ibatis.startTransaction();
			this.ibatis.update("notification.updateReadNotifById", notifId);
			ibatis.commitTransaction();
		}
		catch(SQLException ex){
			ex.printStackTrace();
			cek = false;
		}
		finally{
			try{
				ibatis.endTransaction();
			}
			catch(SQLException e){
				e.printStackTrace();
			}
		}
		return cek;
	}
	
}
